package com.example.gamedemo;

import android.annotation.SuppressLint;
import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Camera;
import android.hardware.camera2.params.OutputConfiguration;
import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;

import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.util.Log;
import android.media.MediaRecorder;
import android.view.View.OnTouchListener;
import android.media.CamcorderProfile;
import android.provider.ContactsContract.Profile;
import java.io.IOException;


/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class FullscreenActivity extends AppCompatActivity {

    private int[] bgID;
    private int[] personID;
    private boolean[] change;
    private boolean[] isDark;
    private boolean[] isUser;
    private boolean[] vibrate;
    private String[] dialogs;
    private boolean mVisible;
    private int slideNum;
    private int maxSlideNum=7;
    private int vibrate_dir=1;
    private int vibrate_count=0;
    private ImageView mBackgroundView;
    private ImageView mPersonView;
    private ImageView mPersonDarkView;
    private FadeInTextView fadeInTextView;
    private ImageButton recordButton;
    MediaRecorder mediaRecorder = new MediaRecorder();
    //= (FadeInTextView) LayoutInflater.from(FullscreenActivity.this).inflate(R.id.dialogView,textView,false);

    private static final boolean AUTO_HIDE = true;
    private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
    private static final int UI_ANIMATION_DELAY = 300;
    private final Handler mHideHandler = new Handler();
    private float scale=(float)(1.0);

    private final Runnable mHidePart2Runnable = new Runnable() {
        @SuppressLint("InlinedApi")
        @Override
        public void run() {
            mBackgroundView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        }
    };
    private final Runnable mHideRunnable = new Runnable() {
        @Override
        public void run() {
            hide();
        }
    };

    private final View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            switch (motionEvent.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    if (AUTO_HIDE) {
                        delayedHide(AUTO_HIDE_DELAY_MILLIS);
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    view.performClick();
                    break;
                default:
                    break;
            }
            return false;
        }
    };

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fullscreen);

        //变量初始化
        mVisible = true;
        slideNum=0;
        Storage storage = new Storage();
        bgID=storage.getBgID();
        personID=storage.getPersonID();
        change=storage.getChange();
        isDark=storage.getIsDark();
        isUser=storage.getIsUser();
        vibrate=storage.getVibrate();
        dialogs=storage.getDialogs();

        mBackgroundView = findViewById(R.id.background);
        mPersonView = findViewById(R.id.person);
        mPersonDarkView = findViewById(R.id.personDark);
        fadeInTextView = findViewById(R.id.dialogView);
        //点击页面全屏或切换slide
        mBackgroundView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clickPage();
            }
        });
        //点击HOME显示底部导航栏
        ImageButton homebutton = findViewById(R.id.homebutton);
        homebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clickToShow();
            }
        });
        //按下record开始录音，松开停止录音
        recordButton = findViewById(R.id.recordButton);
        /*
        OnTouchListener buttonListener = new OnTouchListener() {
            public boolean onTouch(View arg0, MotionEvent event) {
                int action = event.getAction();
                if (action == MotionEvent.ACTION_DOWN) { // 按下
                    try{
                        mediaRecorder.prepare();
                        mediaRecorder.start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                else if (action == MotionEvent.ACTION_UP) { // 松开
                    try{
                        mediaRecorder.stop();
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    }
                }
                return false;
            }
        };
        recordButton.setOnTouchListener(buttonListener);
        */
    }

    private void clickPage() {
        if (mVisible) {
            hide();
        } else {
            nextSlide();
        }
    }
    private void clickToShow(){//如果现在隐藏，则展示
        if(!mVisible){
            show();
        }
    }
    private void nextSlide(){//下一画面
        if(slideNum<maxSlideNum){
            slideNum++;
            refreshSlide();
        }
        else{
            fadeInTextView.stopFadeInAnimation();
            fadeInTextView.setTextString("后续情节尚待更新，敬请期待……");
            fadeInTextView.startFadeInAnimation();
            recordButton.setVisibility(View.INVISIBLE);
            mPersonDarkView.setVisibility(View.VISIBLE);
        }
    }
    private void hide() {//隐藏系统导航栏
        // Hide UI first
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        mVisible = false;
        // Schedule a runnable to remove the status and navigation bar after a delay
        mHideHandler.postDelayed(mHidePart2Runnable, UI_ANIMATION_DELAY);
    }
    private void show() {//Show系统导航栏
        mBackgroundView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
        mVisible = true;
    }

    //打开3s自动全屏
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        delayedHide(3000);
    }
    private void delayedHide(int delayMillis) {
        slideNum=1;
        refreshSlide();
        mHideHandler.removeCallbacks(mHideRunnable);
        mHideHandler.postDelayed(mHideRunnable, delayMillis);
    }

    //刷新页面
    private void refreshSlide(){
        fadeInTextView.stopFadeInAnimation();
        fadeInTextView.setTextString(dialogs[slideNum-1]);
        fadeInTextView.startFadeInAnimation();
        if(change[slideNum-1]){
            Drawable drawableFromResources = ImgHelper.getDrawableFromResources(FullscreenActivity.this, bgID[slideNum-1]);
            mBackgroundView.setImageDrawable(drawableFromResources);
            scale=(float)(1.5);
            mBackgroundView.setScaleX(scale);
            mBackgroundView.setScaleY(scale);
            handler.postDelayed(shrink, 100);//每0.1s执行一次runnable.
        }
        Log.v("slideNum",""+slideNum);
        if(personID[slideNum-1]==0) //没有person人物图片
            mPersonView.setVisibility(View.INVISIBLE);
        else {
            mPersonView.setVisibility(View.VISIBLE);
            mPersonView.setImageResource(personID[slideNum-1]);
            Log.v("personID",""+personID[slideNum-1]);
        }
        if(isDark[slideNum-1]) //人物未说话，为阴影
            mPersonDarkView.setVisibility(View.VISIBLE);
        else {
            mPersonDarkView.setVisibility(View.INVISIBLE);
            Log.v("isDark",""+isDark[slideNum-1]);
        }
        if(isUser[slideNum-1])
            recordButton.setVisibility(View.VISIBLE);
        else
            recordButton.setVisibility(View.INVISIBLE);
        if(vibrate[slideNum-1]){
            vibrate_count=0;
            handler.postDelayed(vibrateRun, 200);
        }
    }
    //计时器
    Handler handler=new Handler();
    //图片缩小
    Runnable shrink=new Runnable() {
        @Override
        public void run() {
            if (scale > 1.0) {
                mBackgroundView.setScaleX(scale);
                mBackgroundView.setScaleY(scale);
                scale -= 0.03;
                handler.postDelayed(this, 100);
            }
        }
    };
    //图片振动
    Runnable vibrateRun=new Runnable() {
        @Override
        public void run() {
            if(vibrate_count<15){
                if (scale < 1.5 && vibrate_dir == 1)
                    scale += 0.2;
                else if(scale > 1 && vibrate_dir == -1)
                    scale -= 0.2;
                else if(scale >= 1.5) {
                    vibrate_dir = -1;
                    scale -= 0.2;
                } else {
                    vibrate_dir = 1;
                    scale += 0.2;
                }
                mBackgroundView.setScaleX(scale);
                mBackgroundView.setScaleY(scale);
                vibrate_count++;
                handler.postDelayed(this, 100);
            }
        }
    };
}